﻿package II.controls {
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import II.util.Eval;
	
	public class UIScrollPanel extends UISinglePanel {

		
		// CONSTANTS //
		
		 public static const EVENT_SCROLL_END:String = "panelScrollEnd";
		
		// CONSTANTS //
		
		public static var TargetScroll:UIScrollPanel = null; 
		
		// PROPERTIES //
		
		public function set isMouseUp ( value:Boolean ) : void {
			if ( value == _isMouseUp ) return;
			if ( value == false ) {
				this.oldX = this.mouseX;
				this.oldY = this.mouseY;
				this.isInGoto = false;
			} else {
				this.isInGoto = true;
			}
			_isMouseUp = value;
		}
		public function get isMouseUp () : Boolean {
			return _isMouseUp;
		}
		private var _isMouseUp:Boolean = true;
		
		
		public function set isInGoto ( value:Boolean ) : void {
			_isInGoto = value;
			if ( _isInGoto == true ) {
				this.addEventListener ( Event.ENTER_FRAME, this.onEnterFrame );
			} else {
				this.speedX = this.speedY = 0;
				this.removeEventListener ( Event.ENTER_FRAME, this.onEnterFrame );
			}
		}
		public function get isInGoto () : Boolean {
			return _isInGoto;
		}
		private var _isInGoto:Boolean = false;
		
		
		public function set pointGoto ( value:Point ) : void {
			_pointGoto = value;
			if ( _pointGoto != null ) this.isInGoto = true;
		}
		public function get pointGoto () : Point {
			return _pointGoto;
		}
		private var _pointGoto:Point = null;
		
		
		public function set snap ( value:Point ) : void {
			_snap = value;
			if ( _snap != null ) this.isInGoto = true;
		}
		public function get snap () : Point {
			return _snap;
		}
		private var _snap:Point = null;
		
		
		public var isScrollHorizontal:Boolean = true;
		public var isScrollVertical:Boolean = true;
		
		// PROPERTIES //
		
		
		
		// PROPERTIES //
		
		private var speedX:Number = 0;
		private var speedY:Number = 0;
		private var oldX:int = 0;
		private var oldY:int = 0;
		
		// PROPERTIES //
		
		
		
		// COSTRUCTOR
		
		public function UIScrollPanel() {
			// constructor code
			
			//this.addEventListener ( MouseEvent.MOUSE_DOWN, this.onMouseDown, false );
			this.addEventListener ( MouseEvent.MOUSE_MOVE, this.onMouseMove, false );
			if (this.stage!=null){
				this.init();
			} else {
				this.addEventListener(Event.ADDED_TO_STAGE, this.init);
			}
		}

		private function init( event:Event=null ) : void {
			this.removeEventListener(Event.ADDED_TO_STAGE, this.init);
			this.stage.addEventListener ( MouseEvent.MOUSE_UP, this.onMouseUp );
		}
		
		// COSTRUCTOR 
		
		
		
		// EVENTS //
		
		private function onMouseDown ( event:MouseEvent ) : void {
			
			
			this.isMouseUp = false;
			event.stopPropagation();
		}
		
		private function onMouseUp ( event:MouseEvent ) : void {
			this.isMouseUp = true;
			//event.stopPropagation();
		}
		
		private function onMouseMove ( event:MouseEvent ) : void {
			var curX:Number = this.mouseX;
			var curY:Number = this.mouseY;
			
			if ( this.isMouseUp == false ) {
				if ( this.isScrollHorizontal == true ) {
					var offsetX:int = curX - this.oldX;
					this.content.x += offsetX;				
					this.speedX = (this.speedX + offsetX) / 2;
					this.oldX = curX;
				}					
				if ( this.isScrollVertical == true ) {
					var offsetY:int = curY - this.oldY;				
					this.content.y += offsetY;
					this.speedY = (this.speedY + offsetY) / 2;
					this.oldY = curY;
				}
			}
			event.stopPropagation();
		}
		
		private function onEnterFrame ( event:Event ) : void {			
//trace("render "+speedY+" :: "+content.size.height+" :: "+this.size.height);		
			
			var offsetX:Number = 0;
			if ( this.pointGoto!=null ) {
				offsetX = -(this.content.x+this.pointGoto.x) * .5;
				this.speedX = 0;
			} else if ( this.content.x > 0 ) {
				offsetX = -this.content.x * .5;
				this.speedX = 0;
			} else if ((this.content.x+this.content.size.width)<this.size.width) {
				var posx:int = 0;
				if ( this.content.size.height>this.size.height ) {
					posx = this.size.width-this.content.size.width;
				}
				offsetX = -(this.content.x-posx) * .5;
				this.speedX = 0;
			} else if ( this.snap != null && Math.abs(this.speedX)<3 ) {
				var posxIndex:int = Math.round( this.content.x / this.snap.x );
				var posxMod:int = this.content.x % this.snap.x;
				if ( posxMod > (this.snap.x/2) ) posx ++;
				var posx:int = posxIndex * this.snap.x;
				offsetX = -(this.content.x-posx) * .5;
				this.speedX = 0;				
			} else {
				offsetX = this.speedX;
				this.speedX = this.speedX * .9;
			}
			this.content.x += offsetX;
			
			
			var offsetY:Number = 0;
			if ( this.pointGoto!=null ) {
				offsetY = -(this.content.y+this.pointGoto.y) * .5;
				this.speedY = 0;
			} else if ( this.content.y > 0 ) {
				offsetY = -this.content.y * .5;
				this.speedY = 0;
			} else if ( (this.content.y+this.content.size.height)<this.size.height ) {
				var posy:int = 0;
				if ( this.content.size.height>this.size.height ) {
					posy = this.size.height-this.content.size.height;
				}
				offsetY = -(this.content.y-posy) * .5;
				this.speedY = 0;
			} else if ( this.snap != null && Math.abs(this.speedY)<3 ) {
				var posyIndex:int = Math.round( this.content.y / this.snap.y );
				var posyMod:int = this.content.y % this.snap.y;
				if ( posyMod > (this.snap.y/2) ) posy ++;
				var posy:int = posyIndex * this.snap.y;
				offsetY = -(this.content.y-posy) * .5;
				this.speedY = 0;				
			} else {
				offsetY = this.speedY;
				this.speedY = this.speedY * .9;
			}
			this.content.y += offsetY;
			
			
			if ( Math.abs(offsetX) < 0.1 && Math.abs(offsetY) < 0.1 ) {
				this.onEndScroll();
			}
			
		}		
		
		protected function onEndScroll() {
			this.isInGoto = false;
			this.pointGoto == null;
			this.dispatchEvent( new Event( UIScrollPanel.EVENT_SCROLL_END ) );
		}
		
		// EVENTS //
		
	}
	
}
